<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="机构名称" prop="fixmedinsName">
        <el-input
          v-model="queryParams.fixmedinsName"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者姓名" prop="patnName">
        <el-input
          v-model="queryParams.patnName"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科室名称" prop="prscDeptName">
        <el-input
          v-model="queryParams.prscDeptName"
          placeholder="请输入开方科室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医师姓名" prop="prscDrName">
        <el-input
          v-model="queryParams.prscDrName"
          placeholder="请输入开方医师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="医生职称名称" prop="drProfttlName">
        <el-input
          v-model="queryParams.drProfttlName"
          placeholder="请输入医生职称名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生科室名称" prop="drDeptName">
        <el-input
          v-model="queryParams.drDeptName"
          placeholder="请输入医生科室名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="特殊病种标志0-否、 1-是" prop="spDiseFlag">
        <el-input
          v-model="queryParams.spDiseFlag"
          placeholder="请输入特殊病种标志0-否、 1-是"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="诊断" prop="maindiagName">
        <el-input
          v-model="queryParams.maindiagName"
          placeholder="请输入诊断"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item
        label="医保费用类别
名称"
        prop="hiFeesetlName"
      >
        <el-input
          v-model="queryParams.hiFeesetlName"
          placeholder="请输入医保费用类别
名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['pr:consultation:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pr:consultation:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pr:consultation:remove']"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['pr:consultation:export']"
          >导出</el-button
        > -->
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="consultationList"
      height="620"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="就诊时间"
        align="center"
        prop="mdtrtTime"
        width="120"
        fixed
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.mdtrtTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patnName" fixed />
      <el-table-column
        label="证件号码"
        align="center"
        prop="certno"
        width="180"
      />
      <el-table-column label="年龄" align="center" prop="patnAge" />
      <el-table-column
        label="机构名称"
        align="center"
        prop="fixmedinsName"
        width="180"
      />
      <el-table-column
        label="机构编号"
        align="center"
        prop="fixmedinsCode"
        width="180"
      />
      <el-table-column
        label="医保就诊ID"
        align="center"
        prop="mdtrtId"
        width="180"
      />
      <el-table-column
        label="门诊/住院号"
        align="center"
        prop="iptOtpNo"
        width="180"
      />
      <el-table-column label="就诊类型" align="center" prop="otpIptFlag">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_visit_type"
            :value="scope.row.otpIptFlag"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="医保人员编号"
        align="center"
        prop="psnNo"
        width="180"
      />
      <el-table-column label="科室" align="center" prop="prscDeptName" />
      <el-table-column label="科室编号" align="center" prop="prscDeptCode" />
      <el-table-column
        label="医保医师代码"
        align="center"
        prop="drCode"
        width="180"
      />
      <el-table-column label="医师姓名" align="center" prop="prscDrName" />

      <el-table-column
        label="医师证件号码"
        align="center"
        prop="prscDrCertno"
        width="180"
      />
      <el-table-column label="医生职称" align="center" prop="drProfttlName" />
      <el-table-column label="科室名称" align="center" prop="drDeptName" />
      <el-table-column
        label="诊断"
        align="center"
        prop="maindiagName"
        width="180"
      />
      <el-table-column
        label="病情描述"
        align="center"
        prop="diseCondDscr"
        width="180"
      />
      <el-table-column
        label="审方药师科室名称"
        align="center"
        prop="pharDeptName"
        width="180"
      />
      <el-table-column
        label="审方药师职称名称"
        align="center"
        prop="pharProfttlName"
        width="180"
      />
      <el-table-column
        label="医保药师代码"
        align="center"
        prop="pharCode"
        width="180"
      />
      <el-table-column
        label="审方药师姓名"
        align="center"
        prop="pharName"
        width="180"
      />
      <el-table-column
        label="药师审方时间"
        align="center"
        prop="pharChkTime"
        width="180"
      />
      <el-table-column
        label="审方药师证件类型"
        align="center"
        prop="pharCertType"
        width="180"
      />
      <el-table-column
        label="审方药师证件号码"
        align="center"
        prop="pharCertno"
        width="180"
      />
      <!-- <el-table-column
        label="医保费用类别名称"
        align="center"
        prop="hiFeesetlName"
        width="180"
      />
      <el-table-column label="挂号费" align="center" prop="rgstFee" /> -->
      <el-table-column
        label="省医保药品库存是否满足原因"
        align="center"
        prop="remarks"
        width="200"
      />
      <el-table-column
        label="审核状态"
        align="center"
        prop="status"
        width="140"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.yy_push_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="取消原因"
        align="center"
        prop="undoRea"
        show-overflow-tooltip
        width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.undoRea }}
        </template>
      </el-table-column>
      <el-table-column
        label="取消处方时间"
        align="center"
        prop="undoTime"
        width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.undoTime }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="220"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="
              openPDF(scope.row.sigUrl, `${scope.row.patnName}处方笺.pdf`)
            "
            v-hasPermi="['pr:consultation:edit']"
            v-if="scope.row.status != 5"
            >处方笺</el-button
          >
          <!-- v-if="scope.row.status == 1||scope.row.status == 2" -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleEditTable(scope.row)"
            v-hasPermi="['pr:consultation:edit']"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-delete"
            style="color: #f56c6c"
            @click="handleCancel(scope.row)"
            v-hasPermi="['pr:consultation:edit']"
            v-if="scope.row.status == 7"
            >取消处方</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handlePush(scope.row)"
            v-hasPermi="['pr:consultation:edit']"
            v-if="scope.row.status == 4"
            >推送记录</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleAudit(scope.row)"
            v-hasPermi="['pr:consultation:add']"
            v-if="
              scope.row.status == 1 ||
              scope.row.status == 2 ||
              scope.row.status == 6
            "
            >药师审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleAudit(scope.row)"
            v-hasPermi="['pr:consultation:export']"
            v-if="
              scope.row.status == 1 ||
              scope.row.status == 2 ||
              scope.row.status == 6
            "
            >用药审核</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改处方流转-规则对话框 -->
    <el-dialog
      title="取消处方"
      :visible.sync="openCancel"
      width="500px"
      append-to-body
    >
      <el-form
        ref="cancelForm"
        :model="cancelForm"
        :rules="cancelrules"
        label-width="80px"
      >
        <el-form-item label="取消原因" prop="undoRea">
          <el-input
            type="textarea"
            v-model="cancelForm.undoRea"
            placeholder="请输入取消原因"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCancelForm">确 定</el-button>
        <el-button @click="cancelPopup">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改处方流转-规则对话框 -->
    <el-dialog
      title="用药提醒"
      :visible.sync="openAudit"
      width="700px"
      append-to-body
    >
      <!-- <el-form
        ref="auditForm"
        :model="auditForm"
        :rules="auditrules"
        label-width="80px"
        style="max-height: 400px;"
      >
        <el-form-item label="处方来源" prop="prescSource">
          <el-select
            v-model="auditForm.prescSource"
            placeholder="请选择处方来源"
          >
            <el-option
              v-for="dict in dict.type.source_of_prescription"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form> -->
      <div style="max-height: 600px; overflow-y: auto">
        <div>当前开具的处方不合理，详细信息如下:</div>
        <br />
        <div v-for="(item, index) in drugList" :key="index">
          <div v-if="item.check_info_list && item.check_info_list.length > 0">
            <div>药品名称:{{ item.drug_lo_name }}</div>
            <br />
            <div v-for="(items, indexs) in item.check_info_list" :key="indexs">
              <div>
                {{ indexs + 1 }}.告警级别:
                <span :style="{ color: colorText(items.color) }">{{
                  items.warning_level
                }}</span>
              </div>
              <br />
              <div>原因:{{ items.name }}</div>
              <br />
              <div>描述:{{ items.ref_source }}{{ items.warning_info }}</div>
              <br />
            </div>
          </div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAuditForm">确 定</el-button>
        <el-button @click="auditPopup">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listConsultation,
  manualPush,
  getConsultation,
  delConsultation,
  addConsultation,
  updateConsultation,
  rxUndo,
  approval,
  submitHlStore,
} from "@/api/pr/consultation";

export default {
  dicts: ["yy_visit_type", "yy_push_status", "source_of_prescription"],
  name: "Consultation",
  data() {
    return {
      openCancel: false,
      cancelInfo: null,
      cancelForm: {
        undoRea: "",
      },
      cancelrules: {
        undoRea: [
          {
            required: true,
            message: "取消原因不能为空",
            trigger: "blur",
          },
        ],
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 处方流转-就诊信息表格数据
      consultationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fixmedinsName: null,
        patnName: null,
        prscDeptName: null,
        prscDrName: null,
        drProfttlName: null,
        drDeptName: null,
        spDiseFlag: null,
        maindiagName: null,
        hiFeesetlName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fixmedinsName: [
          {
            required: true,
            message: "定点医疗机构名称不能为空",
            trigger: "blur",
          },
        ],
        fixmedinsCode: [
          {
            required: true,
            message: "定点医疗机构编号不能为空",
            trigger: "blur",
          },
        ],
        mdtrtId: [
          { required: true, message: "医保就诊 ID不能为空", trigger: "blur" },
        ],
      },
      openAudit: false,
      auditForm: {
        prescSource: "4", //处方来源(0：普通门诊 1：急诊 3：互联网处方 4：处方流转)"
      },
      auditrules: {
        prescSource: [
          {
            required: true,
            message: "请选择处方来源",
            trigger: "change",
          },
        ],
      },
      auditInfo: null,
      drugList: [], //提醒了列表
    };
  },
  created() {
    this.getList();
  },
  methods: {
    colorText(colorText) {
      let color = "#13C2C2";
      if (colorText === "红色") {
        color = "#F56C6C";
      } else if (colorText === "黄色") {
        color = "#E6A23C";
      }
      return color;
    },
    // 获取日期
    getCurrentTime() {
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf =
        new Date().getMinutes() < 10
          ? "0" + new Date().getMinutes()
          : new Date().getMinutes();
      let ss =
        new Date().getSeconds() < 10
          ? "0" + new Date().getSeconds()
          : new Date().getSeconds();
      return `${yy}-${mm}-${dd} ${hh}:${mf}:${ss}`;
    },
    cancelPopup() {
      this.openCancel = false;
      this.cancelInfo = null;
      this.cancelForm.undoRea = "";
      this.resetForm("cancelForm");
    },
    // 取消处方
    handleCancel(row) {
      this.cancelInfo = {
        hospRxno: row.hospRxno,
        fixmedinsCode: row.fixmedinsCode,
        drCode: row.drCode,
        undoDrName: row.prscDrName,
        undoDrCertType: row.psnCertType,
        undoDrCertno: row.psnNo,
      };
      this.openCancel = true;
    },
    /** 提交按钮 */
    submitCancelForm() {
      this.$refs["cancelForm"].validate((valid) => {
        if (valid) {
          rxUndo({
            keys: "nc095fe67ee7358f8fd8e8zk",
            data: {
              undoTime: this.getCurrentTime(),
              ...this.cancelInfo,
              ...this.cancelForm,
            },
          }).then((response) => {
            this.$modal.msgSuccess("取消处方成功");
            this.cancelPopup();
            this.getList();
          });
        }
      });
    },
    openPDF(base64String, filename) {
      const url = "data:application/pdf;base64," + base64String;
      const newWindow = window.open();
      newWindow.document.write(
        '<iframe width="100%" height="100%" src="' + url + '"></iframe>'
      );
    },
    handlePush(row) {
      let param = {
        id: row.id,
        status: "2",
      };
      this.$modal
        .confirm("是否确认推送该记录？")
        .then(() => {
          manualPush(param).then((response) => {
            this.$modal.msgSuccess("推送成功");
            this.getList();
          });
        })
        .catch(() => {});
    },
    // 用药审核
    handleAudit(row) {
      this.auditInfo = {
        id: row.id,
        partner: row.fixmedinsName,
      };
      approval({ ...this.auditInfo, ...this.auditForm }).then((response) => {
        let isshow =
          response.data.data.list.find(
            (element) =>
              element.check_info_list && element.check_info_list.length > 0
          ) !== undefined;
        if (isshow) {
          //用药不合理
          this.openAudit = true;
          this.drugList = response.data.data.list;
        } else {
          //用药合理
          this.$modal
            .confirm("用药合理，是否提交？")
            .then(() => {
              submitHlStore({ ...this.auditInfo, ...this.auditForm }).then(
                (response) => {
                  this.$modal.msgSuccess("提交成功");
                  this.auditPopup();
                  this.getList();
                }
              );
            })
            .catch(() => {});
        }
      });
    },
    //提交审核
    submitAuditForm() {
      this.$refs["auditForm"].validate((valid) => {
        if (valid) {
          submitHlStore({ ...this.auditInfo, ...this.auditForm }).then(
            (response) => {
              this.$modal.msgSuccess("提交成功");
              this.auditPopup();
              this.getList();
            }
          );
        }
      });
    },
    auditPopup() {
      //关闭用药审核
      this.openAudit = false;
      this.auditInfo = null;
      this.auditForm.prescSource = "4";
      this.drugList = [];
      this.resetForm("auditForm");
    },
    /** 修改按钮操作 */
    handleEditTable(row) {
      const id = row.id;
      const params = { pageNum: this.queryParams.pageNum };
      this.$tab.openPage("详情", "/consultation/detail/index/" + id, params);
    },
    /** 查询处方流转-就诊信息列表 */
    getList() {
      this.loading = true;
      listConsultation(this.queryParams).then((response) => {
        this.consultationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        fixmedinsName: null,
        fixmedinsCode: null,
        mdtrtId: null,
        medType: null,
        iptOtpNo: null,
        otpIptFlag: null,
        psnNo: null,
        patnName: null,
        psnCertType: null,
        certno: null,
        patnAge: null,
        patnHgt: null,
        patnWt: null,
        gend: null,
        birctrlType: null,
        birctrlMatnDate: null,
        matnType: null,
        gesoVal: null,
        nwbFlag: null,
        nwbAge: null,
        suckPrdFlag: null,
        algsHis: null,
        prscDeptName: null,
        prscDeptCode: null,
        drCode: null,
        prscDrName: null,
        prscDrCertType: null,
        prscDrCertno: null,
        drProfttlName: null,
        drDeptCode: null,
        drDeptName: null,
        caty: null,
        mdtrtTime: null,
        diseCodg: null,
        diseName: null,
        spDiseFlag: null,
        maindiagCode: null,
        maindiagName: null,
        diseCondDscr: null,
        hiFeesetlType: null,
        hiFeesetlName: null,
        rgstFee: null,
        medfeeSumamt: null,
        fstdiagFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加处方流转-就诊信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getConsultation(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改处方流转-就诊信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateConsultation(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConsultation(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除处方流转-就诊信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delConsultation(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pr/consultation/export",
        {
          ...this.queryParams,
        },
        `consultation_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
